//
//  LXGlobalButton.m
//  UITest_Demo
//
//  Created by Liu on 16/12/19.
//  Copyright © 2016年 Liu. All rights reserved.
//

#import "LXGlobalButton.h"

#define Global_Button_Height 50

@interface LXGlobalButton ()
{
    CGRect _lastFrame;
}

@property (nonatomic, strong) HandleTapGlobalButton callback;
@property (nonatomic, strong) UIPanGestureRecognizer *pan;
@property (nonatomic, strong) UITapGestureRecognizer *tap;

@end

@implementation LXGlobalButton

+ (LXGlobalButton *)showGlobalButtonOnView:(UIView *)view handleTapGlobalButton:(HandleTapGlobalButton)callback {
    LXGlobalButton *button;
    for (UIView *sub in view.subviews) {
        if ([sub isKindOfClass:[LXGlobalButton class]]) {
            button = (LXGlobalButton *)sub;
        }
    }
    if (!button) {
        button = [[LXGlobalButton alloc] initWithFrame:CGRectMake(view.bounds.size.width - Global_Button_Height, view.bounds.size.height/2, Global_Button_Height, Global_Button_Height)];
    }
    button.callback = callback;
    [view addSubview:button];
    return button;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = Global_Button_Height/2;
        self.backgroundColor = [UIColor whiteColor];
        self.image = [UIImage imageNamed:@"leftslider_setting"];
        self.userInteractionEnabled = YES;
        
        self.pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureOnView:)];
        [self addGestureRecognizer:self.pan];
        
        self.tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureOnView:)];
        [self addGestureRecognizer:self.tap];
    }
    return self;
}

- (void)panGestureOnView:(UIPanGestureRecognizer *)pan {
    CGPoint p = [pan translationInView:self];
    
    if (pan.state == UIGestureRecognizerStateBegan) {
        _lastFrame = self.frame;
    }
    else if (pan.state == UIGestureRecognizerStateChanged) {
        CGRect frame = _lastFrame;
        frame.origin.x += p.x;
        frame.origin.y += p.y;
        if (frame.origin.x <= 0) {
            frame.origin.x = 0;
        }
        if (frame.origin.x >= self.superview.bounds.size.width - Global_Button_Height) {
            frame.origin.x = self.superview.bounds.size.width - Global_Button_Height;
        }
        if (frame.origin.y <= 0) {
            frame.origin.y = 0;
        }
        if (frame.origin.y >= self.superview.bounds.size.height - Global_Button_Height) {
            frame.origin.y = self.superview.bounds.size.height - Global_Button_Height;
        }
        self.frame = frame;
    }
    else {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame = self.frame;
            CGFloat x = self.superview.bounds.size.width/2 - frame.origin.x;
            CGFloat y = self.superview.bounds.size.height/2 - frame.origin.y;
            
            if (fabs(x) > fabs(y)) {
                frame.origin.x = x > 0 ? 0 : self.superview.bounds.size.width - Global_Button_Height;
            }
            else {
                frame.origin.y = y > 0 ? 0 : self.superview.bounds.size.height - Global_Button_Height;
            }
            self.frame = frame;
        }];
    }
}

- (void)tapGestureOnView:(UITapGestureRecognizer *)tap {
    if (self.callback) {
        self.callback(self);
    }
}

@end
